package com.yicong.ants.ui.video;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.fragment.BaseStatefulFragment;
import com.cchao.simplelib.view.state.field.FieldStateLayout;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.video.VideoListBean;
import com.yicong.ants.databinding.RecylerBinding;
import com.yicong.ants.ui.video.adapter.VideoHomeAdapter;
import com.yicong.ants.utils.JsonUtils;

/**
 * 主页
 *
 * @author cchao
 */
public class VideoHomeFragment extends BaseStatefulFragment<RecylerBinding> {
    StatefulBindQuickAdapter<VideoListBean> mAdapter;
    RecyclerView mRecycler;
    // 0 喜欢点赞视频 1 转发视频 2 自己的作品
    String mType;
    String mUid;
    int mPageIndex;

    public VideoHomeFragment setPageIndex(int pageIndex) {
        mPageIndex = pageIndex;
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.recyler;
    }

    @Override
    protected void initEventAndData() {
        mType = getArguments().getString(Constants.Extra.Type);
        mUid = getArguments().getString(Constants.Extra.Uid);
        initAdapter();
        onLoadData();
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()){
            case Constants.Event.Video_Del_Update:
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void onLoadData() {
        mAdapter.loadPageData(1);
    }

    private void initAdapter() {
        mRecycler = mDataBind.recycler;
        mRecycler.setBackgroundResource(R.color.black);
        mRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        mAdapter = new VideoHomeAdapter(this, mDisposable)
            .setType(mType)
            .setUserId(mUid);
        mAdapter.bindToRecyclerView(mRecycler);
        mRecycler.setAdapter(mAdapter);

        if (mType.equalsIgnoreCase("2")) {
            View empty = mLayoutInflater.inflate(R.layout.video_home_empty, null);
            empty.findViewById(R.id.publish).setOnClickListener(v -> {
                //
                 Router.turnTo(mContext, VideoPublishActivity.class).start();
            });
            ((FieldStateLayout) mAdapter.mStateView).setViewForState(empty, MultiStateView.VIEW_STATE_EMPTY);
        }

        mAdapter.getEmptyView().setOnClickListener(v -> mAdapter.loadPageData(1));

        mAdapter.setOnItemClickListener((adapter1, view, position) -> {
            Router.turnTo(mContext, VideoListActivity.class)
                .putExtra(Constants.Extra.VIDEO_LIST, JsonUtils.toJson(mAdapter.getData()))
                .putExtra(Constants.Extra.VIDEO_LIST_INDEX, position)
                .putExtra(Constants.Extra.Uid, mUid)
                .putExtra(Constants.Extra.Type, mType)
                .start();
        });
    }
}

